/**
 * 研发流程状态机 - 状态定义
 * 根据研发流程状态机.md文档实现
 */

/**
 * 状态定义配置
 */
const STATES = {
  'PROJECT_REVIEW': {
    name: '项目审核',
    subStates: {
      'DIRECTOR_REVIEW': { name: '董事长' },
      'MANAGER_REVIEW': { name: '研发经理' },
      'ASSIGNMENT': { name: '分配' }
    }
  },
  'DORMANT': {
    name: '休眠',
    subStates: {
      'REVIEW': { name: '审核' },
      'ARCHIVED': { name: '归档1' }
    }
  },
  'ACTIVE': {
    name: '活动',
    subStates: {
      'HARDWARE_CONFIRM': { name: '硬件确认' },
      'DEBUG': { name: '调试' },
      'DATA_SYNC': { name: '资料同步' },
      'TEST': { name: '测试' },
      'CUSTOMER_VALIDATION': { name: '客户验证' },
      'ORDER_PREPARE': { name: '订单准备' },
      'PRODUCTION': { name: '生产' },
      'PENDING_FEEDBACK': { name: '待反馈' }
    }
  }
};

// 硬件子状态
STATES.ACTIVE.subStates.HARDWARE = {
  name: '硬件',
  subStates: {
    'DESIGN': { name: '设计' },
    'REVIEW': { name: '审核1' },
    'SAMPLE': { name: '打样' },
    'SAMPLE_MAKING': { name: '制样' },
    'IN_STOCK': { name: '在库' }
  }
};

// 测试子状态
STATES.ACTIVE.subStates.TEST = {
  name: '测试',
  subStates: {
    'TESTING': { name: '测试中' },
    'NO_TEST_REVIEW': { name: '无测审核' }
  }
};

// 客户确认子状态
STATES.ACTIVE.subStates.CUSTOMER_VALIDATION = {
  name: '客户确认',
  subStates: {
    'SAMPLE_SHIPPING': { name: '寄样' },
    'CUSTOMER_TESTING': { name: '测试1' },
    'SAMPLE_IN_STOCK': { name: '样品在库' },
    'NO_SAMPLE_SIGN_REVIEW': { name: '无签样审核' },
    'ARCHIVING': { name: '归档' }
  }
};

// 无签样审核子状态
STATES.ACTIVE.subStates.CUSTOMER_VALIDATION.subStates.NO_SAMPLE_SIGN_REVIEW = {
  name: '无签样审核',
  subStates: {
    'MANAGER_REVIEW': { name: '研发经理1' },
    'DIRECTOR_REVIEW': { name: '董事长1' }
  }
};

// 订单准备子状态
STATES.ACTIVE.subStates.ORDER_PREPARE = {
  name: '订单准备',
  subStates: {
    'WAITING': { name: '待量产' },
    'REVIEW': { name: '审核2' },
    'DOCUMENT_CONTROL': { name: '文控' },
    'SUPPLEMENT': { name: '补充资料' },
    'PRE_PRODUCTION_MEETING': { name: '产前会' },
    'PRODUCTION_PREPARATION': { name: '生产准备' }
  }
};

// 生产子状态
STATES.ACTIVE.subStates.PRODUCTION = {
  name: '生产',
  subStates: {
    'IN_PRODUCTION': { name: '生产中' },
    'PE_HANDLING': { name: 'PE处理' },
    'RD_INVESTIGATION': { name: '研发排查' },
    'COMPLETED': { name: '完成' }
  }
};

/**
 * 状态编码映射(用于数据库存储)
 */
const STATE_CODES = {
  // 项目审核状态
  'PROJECT_REVIEW': 1,
  'PROJECT_REVIEW.DIRECTOR_REVIEW': { main: 1, sub: 1 },
  'PROJECT_REVIEW.MANAGER_REVIEW': { main: 1, sub: 2 },
  'PROJECT_REVIEW.ASSIGNMENT': { main: 1, sub: 3 },
  
  // 休眠状态
  'DORMANT': 2,
  'DORMANT.REVIEW': { main: 2, sub: 1 },
  'DORMANT.ARCHIVED': { main: 2, sub: 2 },
  
  // 活动状态
  'ACTIVE': 3,
  'ACTIVE.HARDWARE_CONFIRM': { main: 3, active: 'hardware_confirm' },
  'ACTIVE.HARDWARE': { main: 3, active: 'hardware' },
  'ACTIVE.HARDWARE.DESIGN': { main: 3, active: 'hardware', sub: 'design' },
  'ACTIVE.HARDWARE.REVIEW': { main: 3, active: 'hardware', sub: 'review' },
  'ACTIVE.HARDWARE.PURCHASE_SAMPLE': { main: 3, active: 'hardware', sub: 'purchase_sample' },
  'ACTIVE.HARDWARE.SAMPLE_MAKING': { main: 3, active: 'hardware', sub: 'sample_making' },
  'ACTIVE.HARDWARE.STORAGE': { main: 3, active: 'hardware', sub: 'storage' },
  
  'ACTIVE.DEBUG': { main: 3, active: 'debug' },
  'ACTIVE.DATA_SYNC': { main: 3, active: 'data_sync' },
  
  'ACTIVE.TEST': { main: 3, active: 'test' },
  'ACTIVE.TEST.TESTING': { main: 3, active: 'test', sub: 'testing' },
  'ACTIVE.TEST.NO_TEST_REVIEW': { main: 3, active: 'test', sub: 'no_test_review' },
  
  'ACTIVE.CUSTOMER_VALIDATION': { main: 3, active: 'customer_validation' },
  'ACTIVE.CUSTOMER_VALIDATION.SHIPPING': { main: 3, active: 'customer_validation', sub: 'shipping' },
  'ACTIVE.CUSTOMER_VALIDATION.TESTING': { main: 3, active: 'customer_validation', sub: 'testing' },
  'ACTIVE.CUSTOMER_VALIDATION.NO_SIGN_REVIEW': { main: 3, active: 'customer_validation', sub: 'no_sign_review' },
  'ACTIVE.CUSTOMER_VALIDATION.NO_SIGN_REVIEW.MANAGER': { main: 3, active: 'customer_validation', sub: 'no_sign_review.manager' },
  'ACTIVE.CUSTOMER_VALIDATION.NO_SIGN_REVIEW.DIRECTOR': { main: 3, active: 'customer_validation', sub: 'no_sign_review.director' },
  'ACTIVE.CUSTOMER_VALIDATION.SAMPLE_STORAGE': { main: 3, active: 'customer_validation', sub: 'sample_storage' },
  'ACTIVE.CUSTOMER_VALIDATION.ARCHIVED': { main: 3, active: 'customer_validation', sub: 'archived' },
  
  'ACTIVE.ORDER_PREPARE': { main: 3, active: 'order_prepare' },
  'ACTIVE.ORDER_PREPARE.PENDING': { main: 3, active: 'order_prepare', sub: 'pending' },
  'ACTIVE.ORDER_PREPARE.REVIEW': { main: 3, active: 'order_prepare', sub: 'review' },
  'ACTIVE.ORDER_PREPARE.DOC_CONTROL': { main: 3, active: 'order_prepare', sub: 'doc_control' },
  'ACTIVE.ORDER_PREPARE.SUPPLEMENT': { main: 3, active: 'order_prepare', sub: 'supplement' },
  'ACTIVE.ORDER_PREPARE.PRE_MEETING': { main: 3, active: 'order_prepare', sub: 'pre_meeting' },
  'ACTIVE.ORDER_PREPARE.PRODUCTION_PREPARE': { main: 3, active: 'order_prepare', sub: 'production_prepare' },
  
  'ACTIVE.PRODUCTION': { main: 3, active: 'production' },
  'ACTIVE.PRODUCTION.IN_PROGRESS': { main: 3, active: 'production', sub: 'in_progress' },
  'ACTIVE.PRODUCTION.PE_ISSUE': { main: 3, active: 'production', sub: 'pe_issue' },
  'ACTIVE.PRODUCTION.RD_ISSUE': { main: 3, active: 'production', sub: 'rd_issue' },
  
  'ACTIVE.PENDING_FEEDBACK': { main: 3, active: 'pending_feedback' },
  
  // 完成状态
  'COMPLETED': 4
};

/**
 * 获取状态的完整路径名称(用于显示)
 * @param {Object} stateCode 状态编码对象
 * @returns {String} 状态路径名称
 */
function getStateFullName(stateCode) {
  if (!stateCode) return '未知状态';
  
  // 查找匹配的状态定义
  let statePath = '';
  
  // 查找主状态
  if (stateCode.main) {
    for (const [key, value] of Object.entries(STATE_CODES)) {
      if (typeof value === 'number' && value === stateCode.main) {
        statePath = key;
        break;
      }
    }
  }
  
  // 如果有活动状态，添加活动状态
  if (stateCode.active) {
    for (const [key, value] of Object.entries(STATE_CODES)) {
      if (typeof value === 'object' && 
          value.main === stateCode.main && 
          value.active === stateCode.active && 
          !value.sub) {
        statePath = key;
        break;
      }
    }
  }
  
  // 如果有子状态，添加子状态
  if (stateCode.sub) {
    for (const [key, value] of Object.entries(STATE_CODES)) {
      if (typeof value === 'object' && 
          value.main === stateCode.main && 
          (value.active === stateCode.active || (!value.active && !stateCode.active)) && 
          value.sub === stateCode.sub) {
        statePath = key;
        break;
      }
    }
  }
  
  // 将状态路径转换为显示名称
  if (statePath) {
    const parts = statePath.split('.');
    let current = STATES;
    const names = [];
    
    for (let i = 0; i < parts.length; i++) {
      const part = parts[i];
      if (current[part]) {
        names.push(current[part].name);
        current = current[part].subStates || {};
      }
    }
    
    return names.join(' > ');
  }
  
  return '未知状态';
}

/**
 * 根据状态路径获取编码
 * @param {String} statePath 状态路径
 * @returns {Object} 状态编码对象
 */
function getStateCode(statePath) {
  return STATE_CODES[statePath] || null;
}

/**
 * 导出状态定义
 */
module.exports = {
  STATES,
  STATE_CODES,
  getStateFullName,
  getStateCode
}; 